package org.truenewx.tnxjee.repo.mybatis.model.query;

import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import org.truenewx.tnxjee.core.util.LogUtil;
import org.truenewx.tnxjee.model.query.Querying;

public class QueryResultPageProxy<T> extends QueryResultPage<T> implements MethodInterceptor {

    private static final long serialVersionUID = -2184995846104501651L;

    private Logger logger = LogUtil.getLogger(getClass());

    public QueryResultPageProxy(Querying querying) {
        super(querying);
    }

    @Override
    public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        Object target = this;
        // 方法的声明类如果为querying或其父类，则转调querying的方法
        if (method.getDeclaringClass().isAssignableFrom(this.querying.getClass())) {
            target = this.querying;
        }
        Object result = method.invoke(target, args);
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("{}#{} -> {}", target.getClass().getName(), method.getName(), result);
        }
        return result;
    }

}
